/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-03-10
 * Time: 23:20
 */



class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    public int dfs(TreeNode root, int preValue){
        // 该节点为空, 直接返回 0
        if(root==null){
            return 0;
        }
        // 到这个节点后的值
        int sum=preValue*10+root.val;
        // 这个节点就是叶子节点, 直接返回
        if(root.left==null&&root.right==null){
            return sum;
        }
        // 不是叶子节点继续往下遍历, 结果是左右子树的和
        return dfs(root.left, sum) + dfs(root.right, sum);
    }
    public int sumNumbers(TreeNode root) {
        return dfs(root, 0);
    }
}
